import axios from 'axios'
const qs = require('qs')
import { common } from './../vuex/modules/common'

const instance = axios.create()
const DOMAIN_EX = 'exhentai.org'
const DOMAIN_E = 'e-hentai.org'

const HOST_EX = 'https://' + DOMAIN_EX + '/"'
const HOST_E = 'https://' + DOMAIN_E + '/'

export const API_EX = HOST_EX + 'api.php'
export const API_E = HOST_E + 'api.php'

export const URL_FAVORITES_EX = HOST_EX + 'favorites.php'
export const URL_FAVORITES_E = HOST_E + 'favorites.php'

export const API_SIGN_IN = 'https://forums.e-hentai.org/index.php?act=Login&CODE=00'

axios.default.timeout = 5000
axios.defaults.headers.post['Content-Type'] = 'application/json'

instance.defaults.headers.post['Content-Type'] = 'application/json'

axios.interceptors.request.use = instance.interceptors.request.use
instance.interceptors.request.use(config => {
  if (localStorage.getItem('token')) {
    if (config.url.indexOf('api.php') === -1) {
      config.headers.Authorization = `${localStorage.getItem('token').replace(/"/g, '')}`
    }
  }
  return config
}, err => {
  return Promise.reject(err)
})

instance.interceptors.response.use(response => {
  if (response.status === 200) {
    if (response.config.url.indexOf('api.php') > -1) {
      return response.data
    }
    if (response.data.status === 'OK') {
      return response.data
    } else {
      window.notie.alert({
        type: 'error',
        text: response.data.payload
      })
      return Promise.reject(response.data.payload)
    }
  }
}, err => {
  return Promise.reject(err)
})

let url = null

function getHost (isEX) {
  if (isEX) {
    return API_EX
  } else {
    return API_E
  }
}

if (process.env.NODE_ENV === 'development') {
  url = 'http://localhost:8088'
} else if (process.env.NODE_ENV === 'production') {
  url = 'http://cytusian.eicp.net'
}

export const getGalleryList = (params) => {
  let isEX = common.state.isEX
  let searchParams = Object.assign({}, params, {isEX: isEX})
  return instance.get(`${url}/api/e-gallery?${qs.stringify(searchParams)}`)
}

export const getGalleryDetail = (gid, token) => {
  let isEX = common.state.isEX
  return instance.get(`${url}/api/galleryDetail/${gid}/${token}?isEX=${isEX}`)
}

export const getGalleryPerview = (gid, token, pageNo) => {
  let isEX = common.state.isEX
  return instance.get(`${url}/api/galleryDetail/${gid}/${token}?pageNo=${pageNo}&isEX=${isEX}`)
}

export const getGalleryShowKey = (surl) => {
  return instance.get(`${url}/api/galleryKey?url=${surl}`)
}

export const register = (data) => {
  return instance.post(`${url}/register`, data)
}

export const login = (data) => {
  return instance.post(`${url}/login`, data)
}

export const preserveStatus = () => {
  return instance.get(`${url}/api/preserveLoginStatus`)
}

export const getGalleryThumb = (gid, imgKey, page, showKey) => {
  let payload = {
    method: 'showpage',
    gid: gid,
    page: page,
    imgkey: imgKey,
    showkey: showKey
  }
  let isEX = common.state.isEX
  let apiUrl = null
  if (isEX) {
    apiUrl = API_EX
  } else {
    apiUrl = API_E
  }
  return instance.post(`${apiUrl}`, payload)
}

export const getWhatsHot = () => {
  return instance.get(`${url}/api/whatsHot`)
}

export const getListByApi = (array, isEX) => {
  let payload = {
    method: 'gdata',
    gidlist: array,
    namespace: 1
  }
  return instance.post(`${getHost(isEX)}`, payload)
}

export {url}
